﻿using Common.Dtos;
using Newtonsoft.Json;
using Prism.Commands;
using Prism.Events;
using Prism.Ioc;
using Prism.Mvvm;
using Prism.Regions;
using Prism.Services.Dialogs;
using Sigma.Admin.Client.DialogAware;
using Sigma.Admin.Client.Events;
using Sigma.Admin.Client.Services;
using Sigma.Admin.Client.Views;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace Sigma.Admin.Client.ViewModels
{
    public class MainWindowViewModel: BindableBase, IConfigureService
    {
        IEventAggregator _ea;
        private readonly IRegionManager _regionManager;
        private readonly IDialogHostService _dialog;
        private readonly IContainerProvider _containerProvider;



        public MainWindowViewModel(IRegionManager regionManager, IEventAggregator ea, IDialogHostService dialog, IContainerProvider containerProvider)
        {
            _regionManager = regionManager;
            _ea = ea;
            _regionManager.RegisterViewWithRegion("MenuRegion", typeof(MenuRegionView));
            _dialog = dialog;
            _containerProvider= containerProvider;
        }

        #region 搜索栏相关
        private DelegateCommand<string> _searchCommand;
        private string _searchText;
        public string SearchText
        {
            get { return _searchText; }
            set { _searchText = value; RaisePropertyChanged(); }
        }
        private List<DrowDown> _drowDownText=new List<DrowDown>();
        public List<DrowDown> DrowDownText
        {
            get { return _drowDownText; }
            set { _drowDownText = value; 
                RaisePropertyChanged(); 
                    IsDrowDown =true; 
            }
        }
        private bool _isDrowDown = false;
        public bool IsDrowDown
        {
            get { return _isDrowDown; }
            set { _isDrowDown = value; RaisePropertyChanged(); }
        }
        //发布搜索消息
        public DelegateCommand<string> SearchCommand =>
            _searchCommand ?? (_searchCommand = new DelegateCommand<string>(ExecuteSearchCommand));
        private void ExecuteSearchCommand(string text)
        {
            if (!string.IsNullOrEmpty(text))
                _ea.GetEvent<SearchSentEvent>().Publish(new SearchModel() { Message = text, DrowDownsAct = list => DrowDownText = list }); ;
        }
        private DelegateCommand _clearCommand;

        public DelegateCommand ClearCommand =>
            _clearCommand ?? (_clearCommand = new DelegateCommand(ExecuteClearCommand));
        private void ExecuteClearCommand()
        {
            if (!string.IsNullOrEmpty(_searchText))
            {
                SearchText = "";
                DrowDownText.Clear();
            }
                
        }
        #endregion

        #region 导航栏跳转相关
        private DelegateCommand<string> _GoCommand;
        public DelegateCommand<string> GoCommand =>
            _GoCommand ?? (_GoCommand = new DelegateCommand<string>(ExecuteGoCommand));


        private void ExecuteGoCommand(string target)
        {
            _regionManager.RequestNavigate("ContentRegion", target);
        }
        #endregion

        #region 展示token
        private DelegateCommand _showTokenCommand;
        public DelegateCommand ShowTokenCommand => _showTokenCommand ?? (_showTokenCommand = new DelegateCommand(ExecuteShowToken));
        //展示弹窗，不作任何逻辑
        async void ExecuteShowToken()
        {
            DialogParameters param = new DialogParameters();

            var tips = new List<string>();
            var token = AppSession.GetTokenPayload();
            var keys = JsonConvert.DeserializeObject<List<string>>(token.pkey);
            var stringBuilder = new StringBuilder();
            if (keys == null || keys.Count() == 0)
            {
                stringBuilder.Append("无任何权限key");
            }
            else
            {
                keys!.ForEach(x => stringBuilder.Append(x + ","));
                stringBuilder.Remove(stringBuilder.Length - 1, 1);
            }
            tips.Add($"TokenID：{token.id}");
            tips.Add($"颁发者：{token.iss}");
            tips.Add($"消费者：{token.aud}");
            tips.Add($"权限key：{stringBuilder.ToString()}");
            //tips.Add($"签发时间：{TokenDto.GetDateTime(token.iat).ToString("yyyyMMddHHmm")}");
            tips.Add($"发布时间：{TokenDto.GetDateTime(token.iat).ToString("yyyy-MM-dd-HH-mm")}");
            tips.Add($"生效时间：{TokenDto.GetDateTime(token.nbf).ToString("yyyy-MM-dd-HH-mm")}");
            tips.Add($"到期时间：{TokenDto.GetDateTime(token.exp).ToString("yyyy-MM-dd-HH-mm")}");




            var tm = new TipsModel() { Title = "JwtToken安全令牌Payload部分解析", Tips=tips};
            param.Add("Tips", tm);
            ////等待取消
            var dialogResult = await _dialog.ShowDialog("ShowTipsView", param);
        }
        #endregion

        #region 
        private DelegateCommand _exchangeCommand;
        public DelegateCommand ExchangeCommand => _exchangeCommand ?? (_exchangeCommand = new DelegateCommand(ExecuteExchange));

        void ExecuteExchange()
        {
            App.LoginOut(_containerProvider,_regionManager);
        }
        #endregion

        public void Configure()
        {
            _regionManager.RequestNavigate("ContentRegion", nameof(IndexView));
        }
    }


}
